package nancal.mp.db.mongo.entity.dev

import nbcp.base.db.CodeName
import nbcp.base.db.annotation.Cn
import nbcp.base.db.annotation.DbEntityGroup
import nbcp.base.model.ApiBodyParameterDefine
import nbcp.base.model.BaseMarkEntity
import org.springframework.data.mongodb.core.mapping.Document


@Document
@DbEntityGroup("dev")
@Cn("参数模型")
open class ParameterModel(
    @Cn("产品线")
    var productLine: CodeName = CodeName(),
    @Cn("应用")
    var app: String = "",
    var name: String = "",
    var cnName: String = "",
    var define: ApiBodyParameterDefine = ApiBodyParameterDefine(),

    var remark: String = ""
) : BaseMarkEntity() {

    fun joinModelItem(p: ApiBodyParameterDefine, item: ApiBodyParameterDefine) {

        p.type = item.type;
        p.isList = item.isList
        p.referenceModelName = item.referenceModelName
        for (sitem in item.items) {
            var sp = p.items.firstOrNull { it.key == item.key };
            if (sp == null) {
                p.items.add(sitem);
                continue
            }

            joinModelItem(sp, sitem);
        }

        p.enumData = item.enumData;

        if (p.remark.isEmpty()) {
            p.remark = item.remark;
        }
    }

    fun joinModel(model: ParameterModel, override: Boolean) {

        if (override) {
            this.define.items.clear();
        }

        this.define.type = model.define.type;
        this.define.isList = model.define.isList

        for (item in model.define.items) {
            var p = this.define.items.firstOrNull { it.key == item.key };
            if (p == null) {
                this.define.items.add(item);
                continue
            }

            joinModelItem(p, item);
        }
    }
}

